package game.ouroboros.magic;

import java.util.ArrayList;
import java.util.List;

import game.ouroboros.traps.Portao;
import game.ouroboros.traps.Trap;

public class TrapManager implements MapPositions {

	private Trap[][] trapMap;
	public int sizeX;
	public int sizeY;
	public int rows;
	public int cols;

	private int positions[][] = new int[10][2];
	public boolean[][] map;
	private List<Trap> objetivos;

	public TrapManager(int sizeX, int sizeY, int rows, int cols) {
		this.cols = cols;
		this.rows = rows;
		this.sizeX = sizeX;
		this.sizeY = sizeY;
		trapMap = new Trap[rows][cols];
		map = new boolean[rows][cols];
		this.objetivos = new ArrayList<Trap>(3);
	}

	public boolean putTrapInXY(Trap p, float x, float y) {
		int posX = (int) (x / sizeX);
		int posY = (int) (x / sizeY);
		p.position.x = x;
		p.position.y = y;
		p.initialize();
		if (isFree(p, posX, posY)) {
			int count = 0;
			while (positions[count][0] != -1) {
				trapMap[positions[count][0]][positions[count][1]] = p;
				map[positions[count][0]][positions[count][1]] = p.isSolid;
				count++;
			}

			if (p instanceof Portao) {
				objetivos.add(p);
			}
			return true;
		}
		return false;
	}

	private boolean isFree(Trap p, int posX, int posY) {
		if (trapMap[posX][posY] != null)
			return false;
		if ((posX == 0 && p.sizeUnitLeft != 0))
			return false;
		if (posX == rows - 1 && p.sizeUnitRight != 0)
			return false;
		if (posY == 0 && p.sizeUnitUP != 0)
			return false;
		if (posY == cols - 1 && p.sizeUnitDown != 0)
			return false;
		getArchPosition(posX, posY, p);
		int count = 0;
		try {
			while (positions[count][0] != -1) {
				if (trapMap[positions[count][0]][positions[count][1]] != null) {
					return false;
				}

				count++;
			}
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	private void getArchPosition(int posX, int posY, Trap p) {
		int count = 0;
		positions[count][0] = posX;
		positions[count][1] = posY;
		count++;
		if (p.sizeUnitLeft != 0) {
			for (int i = 1; i <= p.sizeUnitLeft; i++) {
				positions[count][0] = posX - i;
				positions[count][1] = posY;
				count++;
			}
		}
		if (p.sizeUnitRight != 0) {
			for (int i = 1; i <= p.sizeUnitRight; i++) {
				positions[count][0] = posX + i;
				positions[count][1] = posY;
				count++;
			}
		}
		if (p.sizeUnitDown != 0) {
			for (int i = 1; i <= p.sizeUnitDown; i++) {
				positions[count][0] = posX;
				positions[count][1] = posY + i;
				count++;
			}
		}

		if (p.sizeUnitUP != 0) {
			for (int i = 1; i <= p.sizeUnitLeft; i++) {
				positions[count][0] = posX;
				positions[count][1] = posY - i;
				count++;
			}
		}
		positions[count][0] = -1;
		positions[count][1] = -1;

	}

	public boolean removeTrapAtXY(int x, int y) {
		int posX = x / sizeX;
		int posY = y / sizeY;
		try {
			Trap p = trapMap[posX][posY];
			getArchPosition(posX, posY, p);
			int count = 0;
			while (positions[count][0] != -1) {
				trapMap[positions[count][0]][positions[count][1]] = null;
				map[positions[count][0]][positions[count][1]] = false;
				count++;
			}
			if (p instanceof Portao) {
				objetivos.remove(p);
			}
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public Trap getTrapAt(int x, int y) {
		try {
			return trapMap[x / sizeX][y / sizeY];
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * TODO - > Luis, aqui vc pode vericar se uma determina posição x,y está
	 * livre
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public boolean isFree(float x, float y) {
		try {
			return (trapMap[(int) (x / sizeX)][(int) (y / sizeY)]) == null;
		} catch (Exception e) {
			return true;
		}
	}

	@Override
	public boolean[][] getMap() {
		return this.map;
	}

	@Override
	public List<Trap> getObjetivos() {
		return this.objetivos;
	}
}